/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.domain.items;

import Server.domain.GameLevel;
import Server.domain.MathHelper;
import Server.domain.Mob;
import Server.domain.Player;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ripper
 */
public class MeleeWeapon extends Weapon
{
    private int pixelRange;
    private int attackAngle; //in degrees
    
    public MeleeWeapon(String itemName, int itemCost, int looktype, int attackValue, int defenseValue, int magicArmorValue, 
            int sqRange, int atkAngle, boolean twoHanded, List<ItemAttribute> attributes)
    {
        super(itemName, itemCost, looktype, attackValue, defenseValue, magicArmorValue, twoHanded, attributes);
        
        setPixelRange(sqRange);
        setAttackAngle(atkAngle);
    }

    public int getPixelRange() { return pixelRange; }
    public final void setPixelRange(int pir) { this.pixelRange = pir; }
    public int getAttackAngle() { return attackAngle; }
    public final void setAttackAngle(int attackAngle) { this.attackAngle = attackAngle; }
    
    @Override
    public Item copyItem()
    {
        List<ItemAttribute> attribs = new ArrayList<ItemAttribute>();
        for(ItemAttribute ia: getEquipmentAttributes())
        {
            attribs.add(new ItemAttribute(ia.getAttribute(), ia.getAttributeValue()));
        }
        
        MeleeWeapon i = new MeleeWeapon(getItemName(), getItemCost(), getLooktype(), 
                getPhysicalAttackValue(), getArmorValue(), getMagicArmorValue(), 
                getPixelRange(), getAttackAngle(), isTwoHandedWeapon(), attribs);
        i.setPartOfSet(getPartOfSet());
        return i;
    }
    @Override
    public void specificAttack(GameLevel gl, Player parent) 
    {
        double directionOfAttack = parent.getAttackDirection();
        //double x = parent.getX();
        //double y = parent.getY();
        
        //TODO
        //detect who is hit based on:
        // -parent's location
        // -pixelrange
        // -attackAngle
        // -parent's attack direction
        
        /*Polygon p = new Polygon();
        p.addPoint((int)parent.getX(),(int)parent.getY());
        
        double dirLeft = directionOfAttack - (attackAngle / 2);
        double dirRight = directionOfAttack + (attackAngle / 2);
        
        for(int a = (int)Math.toDegrees(dirLeft); a < (int)Math.toDegrees(dirRight) ||
                    a < (int)Math.toDegrees(dirRight+(Math.PI*2)); a+= 2)
        {
            p.addPoint((int)((parent.getX()) + pixelRange * Math.cos(a)), 
                    (int)(parent.getY() + pixelRange * Math.sin(a)));
        }*/
        
        List<Mob> monstersHitByAttack = new ArrayList<Mob>();
        double dirLeft = directionOfAttack - (Math.toRadians(attackAngle) / 2);
        double dirRight = directionOfAttack + (Math.toRadians(attackAngle) / 2);
        
        for(Mob m: gl.getMonsters())
        {
            double distance = MathHelper.getDistanceBetween(m, parent);
            
            /*System.out.println("distance needed="+(parent.getHitboxradius()+m.getHitboxradius()+pixelRange)
                    +", distance to this mob ("+m.getMonsterName()+")="+distance);*/
            
            //if monster is in range of the attack
            if(distance <= m.getHitboxradius() + parent.getHitboxradius() + pixelRange)
            {
                double directionToMonster = MathHelper.getAngle(parent, m);
                /*System.out.println(m.getMonsterName()+": dir to monster="+directionToMonster
                        +", dir left="+dirLeft+", dir right="+dirRight);*/
                
                if(dirLeft < directionToMonster && directionToMonster < dirRight)
                {
                    monstersHitByAttack.add(m);
                }
            }
        }
        
        for(Mob m: monstersHitByAttack)
        {
            parent.dealPhysicalDamageToEntity(m, true);
        }
        
        //TODO: fancyfication:
        //      have a lil thing displayed to show the player that an attack has been made
        
    }
    
    @Override
    public String getItemType()
    {
        return "melee weapon";
    }
    
}
